﻿using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Common;
using Lamex.Library.CommonClass;
using Lamex.mIE.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
   public class FeatureCalHelper
   {
        public static void FeatrueStanderCal()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("图号");
            dt.Columns.Add("部件号");
            dt.Columns.Add("工序代码");
            dt.Columns.Add("特征名称");
            dt.Columns.Add("特征取值");
            dt.Columns.Add("特征工时");
            dt.Columns.Add("最后更新时间");
            dt.Columns.Add("更新用户");
            dt.Columns.Add("上载版次");
            dt.Columns.Add("产品类别");
            dt.Columns.Add("单位");
            dt.Columns.Add("值来源");
            dt.Columns.Add("取值逻辑");
            dt.Columns.Add("工时逻辑");
            dt.Columns.Add("特征计算值");
            dt.Columns.Add("工时计算值");
            dt.Columns.Add("单位工时");
            dt.Columns.Add("附加工时");

            using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在计算工时数据...", new Size(400, 80)))
            {
                string errorMessage = "";
                try
                {
                    DataTable dtList = BaseBuildSql.GetDataTableBySql(@"select   a.DWG_NO,a.PART_NO,a.FEATURE_NAME,a.PROCESS_CODE,'' as PROD_CATEGORY,b.UNIT,b.FEAT_VALUE_FROM,b.UNIT_WORKHOURS,b.ATTACH_WORKHOURS,a.FEAT_VALUE,b.FEAT_VALUE_FROM,b.VALUE_LOGIC,b.WORKTIME_LOGIC_CODE from [dbo].[IE_Part_FeatureList] a 
 join  [dbo].[IE_Feature_Definatian]  b on b.FEATURE_NAME = a.FEATURE_NAME");

                    foreach (DataRow dr in dtList.Rows)
                    {

                        string unitWorks = "";
                        string attackWorks = "";
                        string featureValue = "";
                        string featureLogic = "";
                        string workTime = "";
                   
                        errorMessage = dr["DWG_NO"].ToString() + ";" + dr["PART_NO"].ToString() + ";" + dr["FEATURE_NAME"].ToString();
                        //计算单位工时
                        if (dr["UNIT_WORKHOURS"].ToString() == "LGC_UNT_LABOR")
                        {
                            unitWorks = LGC_UNT_LABOR.LGC_UNT_LABOR_VALUE(dr["FEATURE_NAME"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString() == "LGC_UNT_P31_SAW")
                        {
                            unitWorks = LGC_UNT_P31_SAW.LGC_UNT_P31_SAW_VALUE(dr["PART_NO"].ToString()).ToString();
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString().Contains("?"))
                        {
                            unitWorks = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["UNIT_WORKHOURS"].ToString().Trim());

                        }
                        else if (dr["UNIT_WORKHOURS"].ToString().Contains("NIL"))
                        {
                            unitWorks = "NIL";
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString() == "")
                        {
                            unitWorks = "0";
                        }
                        else
                            unitWorks = dr["UNIT_WORKHOURS"].ToString();

                        //计算附加工时
                        if (dr["ATTACH_WORKHOURS"].ToString().Contains("?"))
                        {

                            attackWorks = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["ATTACH_WORKHOURS"].ToString().Trim());

                        }
                        else if (dr["ATTACH_WORKHOURS"].ToString().Contains("NIL"))
                        {
                            attackWorks = "NIL";
                        }
                        else if (dr["ATTACH_WORKHOURS"].ToString() == "")
                        {
                            attackWorks = "0";
                        }
                        else
                            attackWorks = dr["ATTACH_WORKHOURS"].ToString();

                        //计算特征值


                        if (!string.IsNullOrEmpty(dr["VALUE_LOGIC"].ToString()))//计算特征值,以下是公式类型
                        {

                            //计算部件值；
                            if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_S02_S03")
                            {

                                double Value = 0;
                                LGC_VAL_S02_S03.GetSeat_Value(dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), out Value).ToString();
                                featureValue = Value.ToString();
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_BOM")
                            {
                                string errMsg = "";
                                double Value = 0;
                                LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value,out errMsg);
                                featureValue = Value.ToString();
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_T01_BUMPING")
                            {
                                featureValue = LGC_VAL_T01_BUMPING.LGC_VAL_T01_BUMPING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();
                                
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_T03_WELDING")
                            {
                                featureValue = LGC_VAL_T03_WELDING.LGC_VAL_T03_WELDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();
                                
                            }
                            else if (dr["VALUE_LOGIC"].ToString().Contains("?"))
                            {
                                featureValue = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString().Trim());
                              
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "")
                            {
                                featureValue = "0";
                            }
                            else
                            {
                                featureValue = dr["VALUE_LOGIC"].ToString().ToString();
                            }
                            featureLogic = dr["VALUE_LOGIC"].ToString();
                        }
                        else
                        {
                            //计算部件值；
                            if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_S02_S03")
                            {
                                double Value = 0;
                                LGC_VAL_S02_S03.GetSeat_Value(dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), out Value).ToString();
                                featureValue =Math.Round(Value,3).ToString();
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_BOM")
                            {
                                double Value = 0;
                                string errMsg = "";
                                LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value,out errMsg);
                                featureValue = Value.ToString();
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_T01_BUMPING")
                            {
                                featureValue = LGC_VAL_T01_BUMPING.LGC_VAL_T01_BUMPING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString()).ToString();
                                
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_T03_WELDING")
                            {
                                featureValue = LGC_VAL_T03_WELDING.LGC_VAL_T03_WELDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString()).ToString();
                                
                            }
                            else if (dr["FEAT_VALUE"].ToString().Contains("?"))
                            {
                                featureValue = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString().Trim());
                                
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "")
                            {
                                featureValue = "0";
                            }
                            else
                            {
                                featureValue = dr["FEAT_VALUE"].ToString().ToString();
                            }
                            //featureLogic = dr["FEAT_VALUE"].ToString();
                            //featureValue = dr["FEAT_VALUE"].ToString();
                        }
                        //计算工时逻辑
                        //if (featureValue.Contains("L")) featureValue = "0";
                        if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_LABOR")
                        {
                            workTime = LGC_TIM_LABOR.LGC_TIM_LABOR_VALUE(dr["FEATURE_NAME"].ToString(), featureValue).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_T02_BENDING")
                        {
                            workTime = LGC_TIM_T02_BENDING.LGC_TIM_T02_BENDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), featureValue).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_VTA")
                        {
                            double m = Convert.ToDouble(featureValue.ToString());
                            workTime = LGC_TIM_VTA.LGC_TIM_VTA02_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), unitWorks, attackWorks, m).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_VAL_BOM")
                        {
                            double Value = 0;
                            string errMsg = "";
                            LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value,out errMsg);
                            workTime = Value.ToString();
                        }
                        DataRow drow = dt.NewRow();
                        drow["图号"] = dr["DWG_NO"].ToString();
                        drow["部件号"] = dr["PART_NO"].ToString();
                        drow["工序代码"] = dr["PROCESS_CODE"].ToString();
                        drow["特征名称"] = dr["FEATURE_NAME"].ToString();
                        drow["特征取值"] = dr["FEAT_VALUE"].ToString();
                        drow["特征工时"] = "";
                        drow["最后更新时间"] = DateTime.Now;
                        drow["更新用户"] = Loginer.CurrentUser.Account;
                        drow["上载版次"] = "V1.10";
                        drow["产品类别"] = dr["PROD_CATEGORY"].ToString();
                        drow["单位"] = dr["UNIT"].ToString();
                        drow["值来源"] = dr["FEAT_VALUE_FROM"].ToString();
                        drow["取值逻辑"] = dr["VALUE_LOGIC"].ToString();
                        drow["工时逻辑"] = dr["WORKTIME_LOGIC_CODE"].ToString();
                        drow["特征计算值"] = featureValue;
                        drow["工时计算值"] = Math.Round(Convert.ToDouble(workTime), 2);
                        drow["单位工时"] =   unitWorks;
                        drow["附加工时"] = attackWorks;
                        dt.Rows.Add(drow);


                        BaseBuildSql.ExecuteNonQuery(@"update [dbo].[IE_Part_FeatureList] set  LABOR_VALUE='" + Math.Round(Convert.ToDouble(workTime), 2) + "' where PART_NO='" + dr["PART_NO"].ToString() + "' and FEATURE_NAME='" + dr["FEATURE_NAME"].ToString() + "'");

                    }
                    ExecelHelper.DataTableToExcel(dt, @"D:\WorkTime.xlsx");
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message + ":" + errorMessage);
                }

            }
            XtraMessageBox.Show("成功导出！");
        }
        public static void FeatrueBomCal()
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("图号");
            //dt.Columns.Add("部件号");
            //dt.Columns.Add("工序代码");
            //dt.Columns.Add("特征名称");
            //dt.Columns.Add("特征取值");
            //dt.Columns.Add("特征工时");
            //dt.Columns.Add("最后更新时间");
            //dt.Columns.Add("更新用户");
            //dt.Columns.Add("上载版次");
            //dt.Columns.Add("产品类别");
            //dt.Columns.Add("单位");
            //dt.Columns.Add("值来源");
            //dt.Columns.Add("取值逻辑");
            //dt.Columns.Add("工时逻辑");
            //dt.Columns.Add("特征计算值");
            //dt.Columns.Add("工时计算值");
            //dt.Columns.Add("单位工时");
            //dt.Columns.Add("附加工时");
            using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在计算工时数据...", new Size(400, 80)))
            {
                string errorMessage = "";
                try
                {
                    ////UPDATE_USER = 'Steve_成本补数_TPF'
                    DataTable dtMerge = Common.GetTable.GetExcelFormatDataTable();
                    DataTable dtList = BaseBuildSql.GetDataTableBySql(@"select * FROM IE_Part_Routing where UPDATE_USER = 'Robin_成本分析补数'");
                    DataTable featureList = BaseBuildSql.GetDataTableBySql(@"select * from [dbo].[IE_Feature_Definatian]  ");
                    foreach (DataRow dr in dtList.Rows)
                    {
                        if (dr["GROUP_CODE"].ToString() == "P058"||dr["GROUP_CODE"].ToString() == "P055" || dr["GROUP_CODE"].ToString() == "P056" || dr["GROUP_CODE"].ToString() == "P057" || dr["GROUP_CODE"].ToString() == "P059")
                        {
                            double m = 0;
                            string errMsg = "";
                            if (dr["GROUP_CODE"].ToString() != "P058")
                            {
                                string bomTableName = "";
                                if (dr["PART_NO"].ToString().Substring(1, 2) == "96")
                                    bomTableName = "[dbo].[B_IE_PACKAGE_BOM_DATA_SJR]";
                                else
                                    bomTableName = "[dbo].[B_IE_PACKAGE_BOM_DATA_STD]";

                                DataTable dtStdList = BaseBuildSql.GetDataTableBySql(@"select * FROM "+ bomTableName + "  where MATERIAL_NO='" + dr["PART_NO"].ToString() + "' ");
                                if(dtStdList.Rows.Count==0)
                                {
                                    #region""
                                    DataRow drow = dtMerge.NewRow();

                                    drow["图号"] = "NIL";
                                    drow["部件号"] = dr["PART_NO"].ToString();
                                    drow["工序代码"] = "";
                                    drow["特征名称"] = "";
                                    drow["特征取值"] = "";
                                    drow["特征工时"] = "";
                                    drow["最后更新时间"] = DateTime.Now;
                                    drow["更新用户"] = Lamex.Common.Loginer.CurrentUser.Account;
                                    drow["上载版次"] = "V1.10";
                                    drow["产品类别"] = "";
                                    drow["单位"] = "";
                                    drow["值来源"] = "";
                                    drow["取值逻辑"] = "";
                                    drow["工时逻辑"] = "";
                                    drow["特征计算值"] = "0";
                                    drow["工时计算值"] = "900010";//根据特征定义表工时字段计算;
                                    drow["单位工时"] = "";
                                    drow["附加工时"] = "";
                                    //dts.Rows.Add(drow);
                                    dtMerge.Rows.Add(drow);
                                    //DataTable dtCheck = BaseBuildSql.GetDataTableBySql(" select * from [dbo].[IE_Part_FeatureList] where PART_NO = '" + drow["部件号"].ToString() + "' and FEATURE_NAME = '"+ drow["特征名称"] + "'");
                                    //if (dtCheck.Rows.Count == 0)
                                    //{
                                        BaseBuildSql.ExecuteNonQuery(@"insert into [dbo].[IE_Part_FeatureList]
                                                                    (
                                                                    DWG_NO,
                                                                    PART_NO,
                                                                    PROCESS_CODE,
                                                                    FEATURE_NAME,
                                                                    FEAT_VALUE,
                                                                    LABOR_VALUE,
                                                                    UPDATE_TIME,
                                                                    UPDATE_USER
                                                                    )
                                                                    values
                                                                    (
                                                                    '" + drow["图号"].ToString() + @"',
                                                                    '" + drow["部件号"].ToString() + @"',
                                                                    '" + drow["工序代码"] + @"',
                                                                    '" + drow["特征名称"] + @"',
                                                                    '" + drow["特征计算值"] + @"',
                                                                    '" + drow["工时计算值"] + @"',
                                                                    GETDATE(),
                                                                    'Robin_BOM成本分析补数'
                                                                    )");
                                    //}
                                    //else
                                    //{
                                    //    BaseBuildSql.ExecuteNonQuery("update[dbo].[IE_Part_FeatureList] set FEAT_VALUE='" + drow["特征计算值"].ToString() + "', LABOR_VALUE ='" + drow["工时计算值"] + "'  where PART_NO = '" + drow["部件号"].ToString() + "' and FEATURE_NAME = '"+ drow["特征名称"] .ToString()+ "'");
                                    //}
                                    


                                    //
                                    continue;
                                    #endregion
                                }

                            }
                                DataTable dts = LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), dr["GROUP_CODE"].ToString(), "", out m, out errMsg);
                            if (dts.Rows.Count != 0)
                            {
                                foreach (DataRow drs in dts.Rows)
                                {

                                    DataRow row = featureList.Select(" 1=1 and FEATURE_NAME='" + drs["FEATURE_NAME"].ToString() + "'").FirstOrDefault();

                                    //开始计算
                                    DataRow drow = dtMerge.NewRow();
                                    drs["DWG_NO"] = "NIL";
                                    drow["图号"] = drs["DWG_NO"];
                                    drow["部件号"] = drs["PART_NO"];
                                    drow["工序代码"] = drs["PROCESS_CODE"];
                                    drow["特征名称"] = drs["FEATURE_NAME"];
                                    drow["特征取值"] = 1;
                                    drow["特征工时"] = "";
                                    drow["最后更新时间"] = DateTime.Now;
                                    drow["更新用户"] = Loginer.CurrentUser.Account;
                                    drow["上载版次"] = "V1.10";
                                    drow["产品类别"] = "";
                                    drow["单位"] = row["UNIT"];
                                    drow["值来源"] = row["FEAT_VALUE_FROM"];
                                    drow["取值逻辑"] = row["VALUE_LOGIC"];
                                    drow["工时逻辑"] = row["WORKTIME_LOGIC_CODE"];
                                    drow["特征计算值"] = drs["FEAT_VALUE"];
                                    drow["工时计算值"] = drs["LABOR_VALUE"];//根据特征定义表工时字段计算;
                                    drow["单位工时"] = row["UNIT_WORKHOURS"];
                                    drow["附加工时"] = row["ATTACH_WORKHOURS"];
                                    //dts.Rows.Add(drow);
                                    dtMerge.Rows.Add(drow);
                                    //DataTable dtCheck = BaseBuildSql.GetDataTableBySql(" select * from [dbo].[IE_Part_FeatureList] where PART_NO = '" + drow["部件号"].ToString() + "' and FEATURE_NAME = '" + drow["特征名称"] + "'");
                                    //if (dtCheck.Rows.Count == 0)
                                    //{
                                        BaseBuildSql.ExecuteNonQuery(@"insert into [dbo].[IE_Part_FeatureList]
                                                                    (
                                                                    DWG_NO,
                                                                    PART_NO,
                                                                    PROCESS_CODE,
                                                                    FEATURE_NAME,
                                                                    FEAT_VALUE,
                                                                    LABOR_VALUE,
                                                                    UPDATE_TIME,
                                                                    UPDATE_USER
                                                                    )
                                                                    values
                                                                    (
                                                                    '" + drow["图号"].ToString() + @"',
                                                                    '" + drow["部件号"].ToString() + @"',
                                                                    '" + drow["工序代码"] + @"',
                                                                    '" + drow["特征名称"] + @"',
                                                                    '" + drow["特征计算值"] + @"',
                                                                    '" + drow["工时计算值"] + @"',
                                                                    GETDATE(),
                                                                    'Robin_BOM成本分析补数'
                                                                    )");
                                    //}
                                    //else
                                    //{
                                    //    BaseBuildSql.ExecuteNonQuery("update[dbo].[IE_Part_FeatureList] set FEAT_VALUE='"+ drow["特征计算值"].ToString()+ "',  LABOR_VALUE ='" + drow["工时计算值"] + "'  where PART_NO = '" + drow["部件号"].ToString() + "' and FEATURE_NAME = '" + drow["特征名称"].ToString() + "'");
                                    //}

                                }
                            }
                            
                            
                        }
                        
                    }
                    ExecelHelper.DataTableToExcel(dtMerge, @"D:\BOMWorkTime.xlsx");

                    XtraMessageBox.Show("成功导出！");
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message + ":" + errorMessage);
                }
            }
        }

        public static DataTable FeatrueSingleStanderCal(string PartNo)
        {
            DataTable dt = GetTable.GetExcelFormatDataTable();
                string errorMessage = "";
                try
                {
                    DataTable dtList = BaseBuildSql.GetDataTableBySql(@"select   a.DWG_NO,a.PART_NO,a.FEATURE_NAME,a.PROCESS_CODE,'' as PROD_CATEGORY,b.UNIT,b.FEAT_VALUE_FROM,b.UNIT_WORKHOURS,b.ATTACH_WORKHOURS,a.FEAT_VALUE,b.FEAT_VALUE_FROM,b.VALUE_LOGIC,b.WORKTIME_LOGIC_CODE from [dbo].[IE_Part_FeatureList] a 
 join  [dbo].[IE_Feature_Definatian]  b on b.FEATURE_NAME = a.FEATURE_NAME  where a.Part_no='"+ PartNo + "'  ");

                    foreach (DataRow dr in dtList.Rows)
                    {

                        string unitWorks = "";
                        string attackWorks = "";
                        string featureValue = "";
                        string featureLogic = "";
                        string workTime = "";

                        errorMessage = dr["DWG_NO"].ToString() + ";" + dr["PART_NO"].ToString() + ";" + dr["FEATURE_NAME"].ToString();
                        //计算单位工时
                        if (dr["UNIT_WORKHOURS"].ToString() == "LGC_UNT_LABOR")
                        {
                            unitWorks = LGC_UNT_LABOR.LGC_UNT_LABOR_VALUE(dr["FEATURE_NAME"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString() == "LGC_UNT_P31_SAW")
                        {
                            unitWorks = LGC_UNT_P31_SAW.LGC_UNT_P31_SAW_VALUE(dr["PART_NO"].ToString()).ToString();
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString().Contains("?"))
                        {
                            unitWorks = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["UNIT_WORKHOURS"].ToString().Trim());

                        }
                        else if (dr["UNIT_WORKHOURS"].ToString().Contains("NIL"))
                        {
                            unitWorks = "NIL";
                        }
                        else if (dr["UNIT_WORKHOURS"].ToString() == "")
                        {
                            unitWorks = "0";
                        }
                        else
                            unitWorks = dr["UNIT_WORKHOURS"].ToString();

                        //计算附加工时
                        if (dr["ATTACH_WORKHOURS"].ToString().Contains("?"))
                        {

                            attackWorks = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["ATTACH_WORKHOURS"].ToString().Trim());

                        }
                        else if (dr["ATTACH_WORKHOURS"].ToString().Contains("NIL"))
                        {
                            attackWorks = "NIL";
                        }
                        else if (dr["ATTACH_WORKHOURS"].ToString() == "")
                        {
                            attackWorks = "0";
                        }
                        else
                            attackWorks = dr["ATTACH_WORKHOURS"].ToString();

                        //计算特征值


                        if (!string.IsNullOrEmpty(dr["VALUE_LOGIC"].ToString()))//计算特征值,以下是公式类型
                        {

                            //计算部件值；
                            if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_S02_S03")
                            {

                                double Value = 0;
                                LGC_VAL_S02_S03.GetSeat_Value(dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), out Value).ToString();
                                featureValue = Value.ToString();
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_BOM")
                            {
                                string errMsg = "";
                                double Value = 0;
                                LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value, out errMsg);
                                featureValue = Value.ToString();
                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_T01_BUMPING")
                            {
                                featureValue = LGC_VAL_T01_BUMPING.LGC_VAL_T01_BUMPING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();

                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "LGC_VAL_T03_WELDING")
                            {
                                featureValue = LGC_VAL_T03_WELDING.LGC_VAL_T03_WELDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString()).ToString();

                            }
                            else if (dr["VALUE_LOGIC"].ToString().Contains("?"))
                            {
                                featureValue = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString().Trim());

                            }
                            else if (dr["VALUE_LOGIC"].ToString() == "")
                            {
                                featureValue = "0";
                            }
                            else
                            {
                                featureValue = dr["VALUE_LOGIC"].ToString().ToString();
                            }
                            featureLogic = dr["VALUE_LOGIC"].ToString();
                        }
                        else
                        {
                            //计算部件值；
                            if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_S02_S03")
                            {
                                double Value = 0;
                                LGC_VAL_S02_S03.GetSeat_Value(dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), out Value).ToString();
                                featureValue = Math.Round(Value, 3).ToString();
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_BOM")
                            {
                                double Value = 0;
                                string errMsg = "";
                                LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value, out errMsg);
                                featureValue = Value.ToString();
                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_T01_BUMPING")
                            {
                                featureValue = LGC_VAL_T01_BUMPING.LGC_VAL_T01_BUMPING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString()).ToString();

                            }
                            else if (dr["FEAT_VALUE"].ToString() == "LGC_VAL_T03_WELDING")
                            {
                                featureValue = LGC_VAL_T03_WELDING.LGC_VAL_T03_WELDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString()).ToString();

                            }
                            else if (dr["FEAT_VALUE"].ToString().Contains("?"))
                            {
                                featureValue = IELogicHelper.GetLogicValue(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEAT_VALUE"].ToString().Trim());

                            }
                            else if (dr["FEAT_VALUE"].ToString() == "")
                            {
                                featureValue = "0";
                            }
                            else
                            {
                                featureValue = dr["FEAT_VALUE"].ToString().ToString();
                            }
                            //featureLogic = dr["FEAT_VALUE"].ToString();
                            //featureValue = dr["FEAT_VALUE"].ToString();
                        }
                        //计算工时逻辑
                        //if (featureValue.Contains("L")) featureValue = "0";
                        if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_LABOR")
                        {
                            workTime = LGC_TIM_LABOR.LGC_TIM_LABOR_VALUE(dr["FEATURE_NAME"].ToString(), featureValue).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_T02_BENDING")
                        {
                            workTime = LGC_TIM_T02_BENDING.LGC_TIM_T02_BENDING_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), featureValue).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_TIM_VTA")
                        {
                            double m = Convert.ToDouble(featureValue.ToString());
                            workTime = LGC_TIM_VTA.LGC_TIM_VTA02_VALUE(dr["DWG_NO"].ToString(), dr["PART_NO"].ToString(), dr["FEATURE_NAME"].ToString(), unitWorks, attackWorks, m).ToString();
                        }
                        else if (dr["WORKTIME_LOGIC_CODE"].ToString() == "LGC_VAL_BOM")
                        {
                            double Value = 0;
                            string errMsg = "";
                            LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), "", dr["FEATURE_NAME"].ToString(), out Value, out errMsg);
                            workTime = Value.ToString();
                        }
                        DataRow drow = dt.NewRow();
                        drow["图号"] = dr["DWG_NO"].ToString();
                        drow["部件号"] = dr["PART_NO"].ToString();
                        drow["工序代码"] = dr["PROCESS_CODE"].ToString();
                        drow["特征名称"] = dr["FEATURE_NAME"].ToString();
                        drow["特征取值"] = dr["FEAT_VALUE"].ToString();
                        drow["特征工时"] = "";
                        drow["最后更新时间"] = DateTime.Now;
                        drow["更新用户"] = Loginer.CurrentUser.Account;
                        drow["上载版次"] = "V1.10";
                        drow["产品类别"] = dr["PROD_CATEGORY"].ToString();
                        drow["单位"] = dr["UNIT"].ToString();
                        drow["值来源"] = dr["FEAT_VALUE_FROM"].ToString();
                        drow["取值逻辑"] = dr["VALUE_LOGIC"].ToString();
                        drow["工时逻辑"] = dr["WORKTIME_LOGIC_CODE"].ToString();
                        drow["特征计算值"] = featureValue;
                        drow["工时计算值"] = Math.Round(Convert.ToDouble(workTime), 2);
                        drow["单位工时"] = unitWorks;
                        drow["附加工时"] = attackWorks;
                        dt.Rows.Add(drow);



                    }
                return dt;
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message + ":" + errorMessage);
                    return null;
                }

            
            //XtraMessageBox.Show("成功导出！");
        }
        public static DataTable FeatrueSingleBomCal(string PartNo)
        {
            
            string errorMessage = "";
            try
            {
                DataTable dtMerge = Common.GetTable.GetExcelFormatDataTable();
                DataTable dtList = BaseBuildSql.GetDataTableBySql(@"select * FROM IE_Part_Routing where Part_no='"+ PartNo + "' ");
                DataTable featureList = BaseBuildSql.GetDataTableBySql(@"select * from [dbo].[IE_Feature_Definatian]  ");
                foreach (DataRow dr in dtList.Rows)
                {
                    if (dr["GROUP_CODE"].ToString() == "P058" || dr["GROUP_CODE"].ToString() == "P055" || dr["GROUP_CODE"].ToString() == "P056" || dr["GROUP_CODE"].ToString() == "P057" || dr["GROUP_CODE"].ToString() == "P059")
                    {
                        double m = 0;
                        string errMsg = "";
                        if (dr["GROUP_CODE"].ToString() != "P058")
                        {
                            string bomTableName = "";
                            if (dr["PART_NO"].ToString().Substring(1, 2) == "96")
                                bomTableName = "[dbo].[B_IE_PACKAGE_BOM_DATA_SJR]";
                            else
                                bomTableName = "[dbo].[B_IE_PACKAGE_BOM_DATA_STD]";

                            DataTable dtStdList = BaseBuildSql.GetDataTableBySql(@"select * FROM " + bomTableName + "  where MATERIAL_NO='" + dr["PART_NO"].ToString() + "' ");
                            if (dtStdList.Rows.Count == 0)
                            {
                                #region""
                                DataRow drow = dtMerge.NewRow();

                                drow["图号"] = "";
                                drow["部件号"] = dr["PART_NO"].ToString();
                                drow["工序代码"] = dr["GROUP_CODE"].ToString();
                                drow["特征名称"] = "";
                                drow["特征取值"] = "";
                                drow["特征工时"] = "";
                                drow["最后更新时间"] = DateTime.Now;
                                drow["更新用户"] = Loginer.CurrentUser.Account;
                                drow["上载版次"] = "V1.10";
                                drow["产品类别"] = "";
                                drow["单位"] = "";
                                drow["值来源"] = "";
                                drow["取值逻辑"] = "";
                                drow["工时逻辑"] = "";
                                drow["特征计算值"] = "";
                                drow["工时计算值"] = "";//根据特征定义表工时字段计算;
                                drow["单位工时"] = "";
                                drow["附加工时"] = "";
                                //dts.Rows.Add(drow);
                                dtMerge.Rows.Add(drow);
                                continue;
                                #endregion
                            }

                        }
                        DataTable dts = LGC_VAL_BOM.LGC_VAL_BOM_VALUE("","",dr["PART_NO"].ToString(), dr["GROUP_CODE"].ToString(), "", out m, out errMsg);
                        if (dts.Rows.Count != 0)
                        {
                            foreach (DataRow drs in dts.Rows)
                            {

                                DataRow row = featureList.Select(" 1=1 and FEATURE_NAME='" + drs["FEATURE_NAME"].ToString() + "'").FirstOrDefault();

                                //开始计算
                                DataRow drow = dtMerge.NewRow();
                                drow["图号"] = drs["DWG_NO"];
                                drow["部件号"] = drs["PART_NO"];
                                drow["工序代码"] = drs["PROCESS_CODE"];
                                drow["特征名称"] = drs["FEATURE_NAME"];
                                drow["特征取值"] = 1;
                                drow["特征工时"] = "";
                                drow["最后更新时间"] = DateTime.Now;
                                drow["更新用户"] = Loginer.CurrentUser.Account;
                                drow["上载版次"] = "V1.10";
                                drow["产品类别"] = "";
                                drow["单位"] = row["UNIT"];
                                drow["值来源"] = row["FEAT_VALUE_FROM"];
                                drow["取值逻辑"] = row["VALUE_LOGIC"];
                                drow["工时逻辑"] = row["WORKTIME_LOGIC_CODE"];
                                drow["特征计算值"] = drs["FEAT_VALUE"];
                                drow["工时计算值"] = drs["LABOR_VALUE"];//根据特征定义表工时字段计算;
                                drow["单位工时"] = row["UNIT_WORKHOURS"];
                                drow["附加工时"] = row["ATTACH_WORKHOURS"];
                                //dts.Rows.Add(drow);
                                dtMerge.Rows.Add(drow);
                            }
                        }


                    }

                }
                //ExecelHelper.DataTableToExcel(dtMerge, @"D:\BOMWorkTime.xlsx");
                return dtMerge;
                //XtraMessageBox.Show("成功导出！");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message + ":" + errorMessage);
                return null;
            }
        }
        
    }
}
